﻿using System;
using System.Data;
using UniMana.Utils.Data;
using DevExpress.XtraEditors;
using UniMana.Data;
using UniMana.Utils.Configs;

namespace UniMana.Win.Personnel
{
    public partial class FrmPersonnelRegistSubjectTerm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable m_tblTable;
        private DataTable m_tblSubjectTerm;
        private DBMain m_data = new DBMain();
        private Personnel_Personnel_SubjectRegistInfo personSubjectRegistInfo;
        //private DateTime m_dStartTime;
        //private DateTime m_dEndTime;

        public FrmPersonnelRegistSubjectTerm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmPersonnelRegistSubjectTerm_Load(object sender, EventArgs e)
        {
            if (!SysUtils.CheckAccessByRule(RuleConfig.DKMG))
            {
                return;
            }
            try
            {
                LoadPerson();                       // Danh sách giảng viên
                LoadSubjectTerm();                  // Danh sách môn học kỳ tiếp theo
                radTerm.Checked = true;             // Chỉ hiển thị môn giảng viên có thể giảng có trong học kỳ tiếp theo
                grvSubjectTerm.ExpandAllGroups();
                
                rluCatalogue_Department1.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
                rluCatalogue_Department2.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
                rluCatalogue_Department3.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
                dteStart.EditValue = SysDateTime.Now;
                dteEnd.EditValue = SysDateTime.Now + new TimeSpan(90, 0, 0, 0);
            }
            catch { }
        }

        /// <summary>
        /// Hàm lấy danh sách giảng viên
        /// </summary>

        private void LoadPerson()
        {
            try
            {
                m_tblTable = new DataTable();
                m_tblTable = (this.m_data.m_DCReadOnly.Personnel_Personnels.GetListLecturer()).ToDataTable();
                if (m_tblTable.Rows.Count != 0)
                    gluPersonnel_Personnel.GetData(m_tblTable, "Personnel_PersonnelKey", "FullName");
            }
            catch { }
        }

        /// <summary>
        /// Hàm lấy danh sách môn học kỳ tiếp theo
        /// </summary>

        private void LoadSubjectTerm()
        {
            try
            {
                m_tblSubjectTerm = new DataTable();
                m_tblSubjectTerm = (m_data.m_DCReadOnly.Training_Subject_Opens.GetListSubjectOpen(m_data.m_DCReadOnly.Training_Subjects)).ToDataTable();
                if (m_tblSubjectTerm.Rows.Count != 0)
                    gctSubjectTerm.DataSource = m_tblSubjectTerm;
                else
                    gctSubjectTerm.DataSource = null;
            }
            catch { }
        }

        /// <summary>
        /// Hàm lấy danh sách môn giảng viên đã đăng ký
        /// </summary>

        private void LoadSubjectRegist()
        {
            try
            {
                m_tblTable = new DataTable();
                m_tblTable = (this.m_data.m_DCReadOnly.Personnel_Personnel_SubjectRegists.GetSubjectRegist(m_data.m_DCReadOnly.Training_Subjects,
                    m_data.m_DCReadOnly.Training_Subject_Opens,
                    gluPersonnel_Personnel.EditValue.ToString(),
                    SysDateTime.Now.Date)).ToDataTable();
                if (m_tblTable.Rows.Count != 0)
                    gctSubjectRegist.DataSource = m_tblTable;
                else
                    gctSubjectRegist.DataSource = null;
            }
            catch { }
        }

        /// <summary>
        /// Hàm lấy danh sách môn chưa đăng ký
        /// </summary>

        private void LoadSubjectNotRegist()
        {
            try
            {
                m_tblTable = new DataTable();
                m_tblTable = (this.m_data.m_DCReadOnly.Personnel_Personnel_Subjects.GetSubjectNotRegist(m_data.m_DCReadOnly.Personnel_Personnel_SubjectRegists,
                    m_data.m_DCReadOnly.Training_Subjects,
                    gluPersonnel_Personnel.EditValue.ToString())).ToDataTable();
                if (m_tblTable.Rows.Count != 0)
                {
                    gctSubjectNotRegist.DataSource = m_tblTable;
                }
                else
                {
                    gctSubjectNotRegist.DataSource = null;
                }
            }
            catch { }
        }

        /// <summary>
        /// Hàm lấy danh sách môn chưa đăng ký có trong học kỳ tiếp theo
        /// </summary>

        private void LoadSubjectNotRegistInTerm()
        {
            try
            {
                m_tblTable = new DataTable();
                m_tblTable = (this.m_data.m_DCReadOnly.Personnel_Personnel_Subjects.GetSubjectNotRegistInTerm(m_data.m_DCReadOnly.Personnel_Personnel_SubjectRegists,
                    m_data.m_DCReadOnly.Training_Subject_Opens,
                    m_data.m_DCReadOnly.Training_Subjects,
                    gluPersonnel_Personnel.EditValue.ToString())).ToDataTable();
                if (m_tblTable.Rows.Count != 0)
                {
                    gctSubjectNotRegist.DataSource = m_tblTable;
                }
                else
                {
                    gctSubjectNotRegist.DataSource = null;
                }
            }
            catch { }
        }

        /// <summary>
        /// Hàm hiển thị thông tin tất cả danh sách môn
        /// </summary>

        private void DisPlaySubjectPersonRegist()
        {
            try
            {
                if (gluPersonnel_Personnel.EditValue != null)
                {
                    if (radTerm.Checked == true)
                        LoadSubjectNotRegistInTerm();
                    if (radAll.Checked == true)
                        LoadSubjectNotRegist();
                    LoadSubjectRegist();
                    grvSubjectRegist.ExpandAllGroups();
                    grvSubjectNotRegist.ExpandAllGroups();
                }
            }
            catch { }
        }

        /// <summary>
        /// Thay đổi giảng viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gluPersonnel_Personnel_EditValueChanged(object sender, EventArgs e)
        {
            DisPlaySubjectPersonRegist();
        }

        /// <summary>
        /// Chọn môn chưa đăng ký có trong học kỳ tiếp theo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void radTerm_CheckedChanged(object sender, EventArgs e)
        {
            DisPlaySubjectPersonRegist();
        }

        /// <summary>
        /// Chọn tất cả các môn chưa đăng ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void radAll_CheckedChanged(object sender, EventArgs e)
        {
            DisPlaySubjectPersonRegist();
        }

        /// <summary>
        /// Kiểm tra môn đăng ký phải được mở trong học kỳ tiếp theo
        /// </summary>
        /// <param name="m_tblSubjectTerm"></param>
        /// <param name="m_iSubjectKey"></param>
        /// <returns></returns>

        private bool CheckInsert(DataTable m_tblSubjectTerm, int m_iSubjectKey)
        {
            if (m_tblSubjectTerm.Rows.Count != 0)
            {
                for (int i = 0; i < m_tblSubjectTerm.Rows.Count; i++)
                {
                    if (m_iSubjectKey == m_tblSubjectTerm.Rows[i]["Training_SubjectKey"].ToInt())
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Hàm đăng ký môn
        /// </summary>

        private void AddSubject()
        {
            try
            {

                if (grvSubjectNotRegist.SelectedRowsCount > 0)
                {
                    if ((DateTime)dteEnd.EditValue > (DateTime)dteStart.EditValue)
                    {
                        int[] index = grvSubjectNotRegist.GetSelectedRows();
                        for (int i = 0; i < index.Length; i++)
                        {
                            if (CheckInsert(m_tblSubjectTerm, this.grvSubjectNotRegist.GetRowCellValue(index[i], "Training_SubjectKey").ToInt()))
                            {
                                personSubjectRegistInfo = new Personnel_Personnel_SubjectRegistInfo(gluPersonnel_Personnel.EditValue.ToString(), this.grvSubjectNotRegist.GetRowCellValue(index[i], "Training_SubjectKey").ToInt(), SysDateTime.Now.Date);//, m_dStartTime, m_dEndTime
                                personSubjectRegistInfo.Properties.Personnel_PersonnelKey = gluPersonnel_Personnel.EditValue.ToGuid();
                                personSubjectRegistInfo.Properties.Training_SubjectKey = this.grvSubjectNotRegist.GetRowCellValue(index[i], "Training_SubjectKey").ToInt();
                                personSubjectRegistInfo.Properties.StartTime = (DateTime)dteStart.EditValue;
                                personSubjectRegistInfo.Properties.EndTime = (DateTime)dteEnd.EditValue;
                                personSubjectRegistInfo.Properties.Status = false;
                                personSubjectRegistInfo.Save();
                            }
                            else
                                XtraMessageBox.Show("Môn " + this.grvSubjectNotRegist.GetRowCellValue(index[i], "Name") + " không có trong danh sách môn mở. Không thể đăng ký", "Thông báo");
                        }
                        DisPlaySubjectPersonRegist();
                    }
                    else
                        XtraMessageBox.Show("Ngày bắt đầu phải nhỏ hơn Ngày kết thúc");
                }
                else
                    XtraMessageBox.Show("Chưa chọn môn", "Thông báo");
            }
            catch { }
        }

        /// <summary>
        /// Hàm hủy đăng ký môn
        /// </summary>

        private void RemoveSubject()
        {
            try
            {
                if (grvSubjectRegist.SelectedRowsCount > 0)
                {
                    int[] index = grvSubjectRegist.GetSelectedRows();
                    for (int i = 0; i < index.Length; i++)
                    {
                        personSubjectRegistInfo = new Personnel_Personnel_SubjectRegistInfo(gluPersonnel_Personnel.EditValue.ToString(), this.grvSubjectRegist.GetRowCellValue(index[i], "Training_SubjectKey").ToInt(), SysDateTime.Now.Date);//, m_dStartTime, m_dEndTime
                        if (personSubjectRegistInfo.Properties.Personnel_Personnel_SubjectRegistKey > 0)
                            personSubjectRegistInfo.Delete();
                    }
                    DisPlaySubjectPersonRegist();
                }
                else
                    XtraMessageBox.Show("Chưa chọn môn", "Thông báo");
            }
            catch { }
        }

        /// <summary>
        /// Thao tác đăng ký môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddSubject();
        }

        /// <summary>
        /// Thao tác hủy đăng ký môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnRemove_Click(object sender, EventArgs e)
        {
            RemoveSubject();
        }

        private void radTerm_MouseHover(object sender, EventArgs e)
        {
            this.radTerm.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Underline))));
        }

        private void radTerm_MouseLeave(object sender, EventArgs e)
        {
            this.radTerm.Font = new System.Drawing.Font("Tahoma", 8.25F);
        }

        private void radAll_MouseHover(object sender, EventArgs e)
        {
            this.radAll.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Underline))));
        }

        private void radAll_MouseLeave(object sender, EventArgs e)
        {
            this.radAll.Font = new System.Drawing.Font("Tahoma", 8.25F);
        }
    }
}
